//
//  YKClientDataDetailEditViewController.m
//  YunKang
//
//  Created by leoking870 on 2017/10/1.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKClientDataDetailEditViewController.h"
#import "YKInputView.h"
#import "YKSelectView.h"
#import "YKMultiSelectDropdownView.h"
#import "YKInsetTextField.h"
#import "YKInputMultilineView.h"
#import "YKMedQuaModel.h"
#import "YKSettlemenTypeModel.h"
#import <IQPreviousNextView.h>

@interface YKClientDataDetailEditViewController ()
@property(nonatomic, strong) UIScrollView *scrollView;
@property(nonatomic, strong) YKInputView *drugCodeView;
@property(nonatomic, strong) YKInputView *clientNameView;

@property(nonatomic, strong) YKSelectView* categoryView;
//@property (nonatomic, strong) YKMultiSelectDropdownView *multiSelectView1;
@property(nonatomic, strong) YKInputView *detailAddressTF;
@property(nonatomic, strong) YKInputView *registerMoneyView;
@property(nonatomic, strong) YKInputView *contactView;
@property(nonatomic, strong) YKInputView *phoneNumberView;
@property(nonatomic, strong) YKInputView *foundDateView;
@property(nonatomic, strong) YKInputView *doctorCountView;
@property(nonatomic, strong) YKInputView *nerseCountView;
@property(nonatomic, strong) YKSelectView *typeView;
@property(nonatomic, strong) YKSelectView *insuranceView;

@property(nonatomic, strong) YKInputMultilineView *saleDrugsView;
@property(nonatomic, strong) YKInputMultilineView *yearBuyView;
@property(nonatomic, strong) YKInputMultilineView *yearSaleView;
@property(nonatomic, strong) YKInputMultilineView *currentChannelView;


/**
 已收账款
 */
@property(nonatomic, strong) YKInputView *yishouMoneyView;
/**
 应收账款
 */
@property(nonatomic, strong) YKInputView *yinshouMoneyView;
@end

@implementation YKClientDataDetailEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"维护客户资料";
    self.view.backgroundColor = [UIColor whiteColor];

    self.scrollView = [[UIScrollView alloc] init];
    IQPreviousNextView *contentView = [[IQPreviousNextView alloc] init];
    [self.view addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [contentView addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.trailing.equalTo(contentView);
        make.bottom.equalTo(contentView.mas_bottom).offset(-44);
    }];

    UIButton *saveButton = [[UIButton alloc] init];
    [self.view addSubview:saveButton];
    UIButton *cancleButton = [[UIButton alloc] init];
    [self.view addSubview:cancleButton];
    UIButton *submitButton = [[UIButton alloc] init];
//    [self.view addSubview:submitButton];
    [saveButton setTitle:@"保存" forState:UIControlStateNormal];
    [cancleButton setTitle:@"取消" forState:UIControlStateNormal];
    [submitButton setTitle:@"提交审批" forState:UIControlStateNormal];
    [saveButton setBackgroundColor:TFSTYLE(buttonEnableBackgroundColor)];
    [cancleButton setBackgroundColor:TFSTYLE(buttonEnableBackgroundColor)];
    [submitButton setBackgroundColor:TFSTYLE(buttonEnableBackgroundColor)];
    [saveButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    [cancleButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    [submitButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    saveButton.titleLabel.font = [UIFont systemFontOfSize:14];
    cancleButton.titleLabel.font = [UIFont systemFontOfSize:14];
    submitButton.titleLabel.font = [UIFont systemFontOfSize:14];

    [saveButton addTarget:self action:@selector(onSaveButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [cancleButton addTarget:self action:@selector(onCancleButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [submitButton addTarget:self action:@selector(onSubmitButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

    [@[saveButton, cancleButton] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:1 leadSpacing:0 tailSpacing:0];
    [@[saveButton, cancleButton] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view);
        make.height.mas_equalTo(44);
    }];


    YKInputView *drugCodeView = [[YKInputView alloc] initWithTitle:@"客户编码" input:self.mYKClientDataDetail.C_CODE];
    self.drugCodeView = drugCodeView;
    [self.scrollView addSubview:drugCodeView];

    YKInputView *clientNameView = [[YKInputView alloc] initWithTitle:@"客户名称" input:self.mYKClientDataDetail.C_NAME];
    self.clientNameView = clientNameView;
    [self.scrollView addSubview:clientNameView];


    NSArray *data = @[
            @[@"医疗器械", @"医疗器械1", @"医疗器械2", @"医疗器械3"],
            @[@"医疗器械", @"医疗器械1", @"医疗器械2", @"医疗器械3"],
            @[@"医疗器械", @"医疗器械1", @"医疗器械2", @"医疗器械3"]
    ];

    YKTypeModel *typeModels = [[[CommonDataManager sharedInstance].customerTypeList tf_filteredWithPredicateBlock:^BOOL(YKTypeModel *  _Nonnull obj, NSInteger idx) {
        return [self.mYKClientDataDetail.CUSTOMERTYPE isEqualToString:obj.id_p] || [self.mYKClientDataDetail.CUSTOMERTYPE isEqualToString:obj.code];
    }]firstObject];
    YKSelectView* categoryView = [[YKSelectView alloc] initWithTitle:@"客户分类" row:(NSArray <YKDropdown> *) [CommonDataManager sharedInstance].customerTypeList initialText:typeModels];
    [self.scrollView addSubview:categoryView];
    self.categoryView = categoryView;

    YKSettlemenTypeModel *settlemenTypeModel = [[[CommonDataManager sharedInstance].settlemenTypeList tf_filteredWithPredicateBlock:^BOOL(YKSettlemenTypeModel *_Nonnull obj, NSInteger idx) {
        return [self.mYKClientDataDetail.SETTLEMENTTYPE isEqualToString:obj.id_p] || [self.mYKClientDataDetail.SETTLEMENTTYPE isEqualToString:obj.code];
    }] firstObject];
    
    YKSelectView *typeView = [[YKSelectView alloc] initWithTitle:@"结算类型" row:(NSArray <YKDropdown> *) [CommonDataManager sharedInstance].settlemenTypeList initialText:settlemenTypeModel];
    [self.scrollView addSubview:typeView];
    
    YKMedQuaModel *selectedMedQuaModel = [[[CommonDataManager sharedInstance].medQuaList tf_filteredWithPredicateBlock:^BOOL(YKMedQuaModel *_Nonnull obj, NSInteger idx) {
        return [obj.id_p isEqualToString:self.mYKClientDataDetail.MEDICAL_QUALIFICATION] || [obj.code isEqualToString:self.mYKClientDataDetail.MEDICAL_QUALIFICATION];
    }] firstObject];
    self.typeView = typeView;
    YKSelectView *insuranceView = [[YKSelectView alloc] initWithTitle:@"医保资质" row:(NSArray <YKDropdown> *) [CommonDataManager sharedInstance].medQuaList initialText:selectedMedQuaModel];
    [self.scrollView addSubview:insuranceView];
    self.insuranceView = insuranceView;
    
//    YKMultiSelectDropdownView *multiSelectView1 = [[YKMultiSelectDropdownView alloc]initWithTitle:@"区域定位"
//                                                                                             rows:@[@[@"江苏省"],@[@"南京市"],@[@"秦淮区"]]];
//    [self.scrollView addSubview:multiSelectView1];
//    self.multiSelectView1 = multiSelectView1;



    [self.scrollView.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.scrollView);
        make.width.mas_equalTo(self.scrollView.mas_width);
    }];
//


    YKInputView *yinshouMoneyView = [[YKInputView alloc] initWithTitle:@"累积订单" input:self.mYKClientDataDetail.LJDD_SUM];

    [self.scrollView addSubview:yinshouMoneyView];
    self.yinshouMoneyView = yinshouMoneyView;
    YKInputView *yishouMoneyView = [[YKInputView alloc] initWithTitle:@"累积收款" input:self.mYKClientDataDetail.LJSK_SUM];

    [self.scrollView addSubview:yishouMoneyView];
    self.yishouMoneyView = yishouMoneyView;


    YKInputView *contactView = [[YKInputView alloc] initWithTitle:@"联系人" input:self.mYKClientDataDetail.CONSIGNEE];
    [self.scrollView addSubview:contactView];
    self.contactView = contactView;
    YKInputView *phoneNumberView = [[YKInputView alloc] initWithTitle:@"手机号码" input:self.mYKClientDataDetail.CONSIGNEE_PHONE];
    [self.scrollView addSubview:phoneNumberView];
    self.phoneNumberView = phoneNumberView;
    
    
    YKInputView* detailAddressTF = [[YKInputView alloc]initWithTitle:@"详细地址" input:self.mYKClientDataDetail.RECEIVING_DETAILADDRESS];
    [self.scrollView addSubview:detailAddressTF];
    
    
    
    self.detailAddressTF = detailAddressTF;
    

    YKInputView *registerMoneyView = [[YKInputView alloc] initWithTitle:@"注册资金" input:self.mYKClientDataDetail.REG_CAPITAL];
    self.registerMoneyView = registerMoneyView;
    [self.scrollView addSubview:registerMoneyView];

    YKInputView *foundDateView = [[YKInputView alloc] initWithTitle:@"成立时间" input:self.mYKClientDataDetail.ESTABLISH];
    self.foundDateView = foundDateView;
    self.foundDateView.type = YKInputViewTypeDate;
    self.foundDateView.dateFormat = @"yyyy-MM-dd";
    [self.scrollView addSubview:foundDateView];

    YKInputView *doctorCountView = [[YKInputView alloc] initWithTitle:@"医生数量" input:self.mYKClientDataDetail.DOCTORCOUNT];
    self.doctorCountView = doctorCountView;
    [self.scrollView addSubview:doctorCountView];

    YKInputView *nerseCountView = [[YKInputView alloc] initWithTitle:@"护工数量" input:self.mYKClientDataDetail.NURSECOUNT];
    self.nerseCountView = nerseCountView;
    [self.scrollView addSubview:nerseCountView];


    YKInputMultilineView *currentChannelView = [[YKInputMultilineView alloc] initWithTitle:@"现有渠道" input:self.mYKClientDataDetail.PURCHASING_CHANNEL];
    [self.scrollView addSubview:currentChannelView];


    YKInputMultilineView *saleDrugsView = [[YKInputMultilineView alloc] initWithTitle:@"在售主流药品" input:self.mYKClientDataDetail.MAINSTREAM_DRUGS];
    [self.scrollView addSubview:saleDrugsView];

    YKInputMultilineView *yearBuyView = [[YKInputMultilineView alloc] initWithTitle:@"年采购情况" input:self.mYKClientDataDetail.PURCHASING_SITUATION];
    [self.scrollView addSubview:yearBuyView];

    YKInputMultilineView *yearSaleView = [[YKInputMultilineView alloc] initWithTitle:@"年销售情况" input:self.mYKClientDataDetail.SALES_SITUATION];
    [self.scrollView addSubview:yearSaleView];

    self.saleDrugsView = saleDrugsView;
    self.yearBuyView = yearBuyView;
    self.yearSaleView = yearSaleView;
    self.currentChannelView = currentChannelView;

    [@[yinshouMoneyView, yishouMoneyView, contactView, phoneNumberView, detailAddressTF, registerMoneyView, foundDateView, doctorCountView, nerseCountView, typeView, insuranceView, currentChannelView, saleDrugsView, yearBuyView,categoryView, yearSaleView] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.scrollView);
        make.width.mas_equalTo(self.scrollView.mas_width);
    }];

    [self.scrollView.subviews mas_alignAlongAxis:MASAxisTypeVertical fixedSpace:@0 leadSpace:8 tailSpace:8];

//    multiSelectView1.modelData = [CommonDataManager sharedInstance].customerRegionList;
//    multiSelectView1.selectedCodes = [self.mYKClientDataDetail.REGION componentsSeparatedByString:@"|"];

    [self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView *_Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
        if ([obj isKindOfClass:[YKInputView class]]) {
            YKInputView *inputView = obj;
            inputView.editable = YES;
        } else if ([obj isKindOfClass:[YKInputMultilineView class]]) {
            YKInputMultilineView *multiInputView = obj;
            multiInputView.editable = YES;
        } else if ([obj isKindOfClass:[YKSelectView class]]) {
            YKSelectView *selectView = obj;
            selectView.enabled = YES;
        } else if ([obj isKindOfClass:[YKMultiSelectDropdownView class]]) {
            YKMultiSelectDropdownView *selectView = obj;
            selectView.enabled = YES;
        }
    }];
    self.drugCodeView.editable = NO;
    self.detailAddressTF.editable = NO;
    yinshouMoneyView.editable = NO;
    yishouMoneyView.editable = NO;
//    categoryView.enabled = NO;
}

- (void)onSaveButtonTapped:(id)sender {
    NSString *id_p = self.mYKClientDataDetail.ID;
    NSString *c_name = self.clientNameView.input;
//    NSString *address = self.detailAddressTF.text;
    NSString *reg_capital = self.registerMoneyView.input;
    NSString *establish = self.foundDateView.input;
    NSString *doctorcount = self.doctorCountView.input;
    NSString *nursecount = self.nerseCountView.input;
    NSString *purchasing_channel = self.currentChannelView.input;
    NSString *mainstream_drugs = self.saleDrugsView.input;
    NSString *purchasing_situation = self.yearBuyView.input;
    NSString *sales_situation = self.yearSaleView.input;

    

    NSString *settlementtype = [self.typeView selectedRow].id_p;
    NSString *medical_qualification = [self.insuranceView selectedRow].id_p;
    NSString *consignee = self.contactView.input;
    NSString *consignee_phone = self.phoneNumberView.input;
    NSString *receiving_detailaddress = self.detailAddressTF.input;
//    NSArray<YKDropdown> *regionModels = [self.multiSelectView1 selectedModels];
//    NSString *regionArry = [[regionModels tf_mapUsingBlock:^id _Nullable(id<YKDropdown>  _Nonnull obj, NSInteger idx) {
//        return obj.code;
//    }]componentsJoinedByString:@"|"];
//    NSString *regionNameArry = [[regionModels tf_mapUsingBlock:^id _Nullable(id<YKDropdown>  _Nonnull obj, NSInteger idx) {
//        return obj.name;
//    }]componentsJoinedByString:@"-"];


    [TFHTTPMANAER POST:@"customerDataManager/edtCustomerDataManager.htm" parameters:@{
            @"id": id_p,
            @"c_name": c_name,
            @"address": @"",
            @"reg_capital": reg_capital,
            @"establish": establish,
            @"doctorcount": doctorcount,
            @"nursecount": nursecount,
            @"purchasing_channel": purchasing_channel,
            @"mainstream_drugs": mainstream_drugs,
            @"purchasing_situation": purchasing_situation,
            @"sales_situation": sales_situation,
            @"categoryArry": @"",
            @"categoryNameArry": @"",
            @"settlementtype": settlementtype,
            @"medical_qualification": medical_qualification,
//                                        @"regionArry":regionArry,
//                                        @"regionNameArry":regionNameArry,
            @"consignee_phone": consignee,
            @"consignee": consignee_phone,
            @"receiving_detailaddress": receiving_detailaddress,
            @"customertype":self.categoryView.selectedRow.id_p?:@""
    }              HUD:@"修改中..." success:^(id responseObject) {
        if (self.delegate) {
            [self.delegate clientDataDetailEditSuccess];
        }
        [self showHint:@"修改成功"];
        [self.navigationController popViewControllerAnimated:YES];
    }          failure:^(id responseObject, NSError *error) {
        [self showHint:@"修改失败"];
    }];

}

- (void)onCancleButtonTapped:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)onSubmitButtonTapped:(id)sender {

}

@end
